package com.warrantyhotel.android.ui;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Handler;
import android.provider.BaseColumns;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.warrantyhotel.android.R;
import com.warrantyhotel.android.adapter.InboxAdapter;
import com.warrantyhotel.android.db.QueriesCodes;
import com.warrantyhotel.android.db.WarrantyHotelDBSchema.Inbox_Account_Upload_Transfers_Table;
import com.warrantyhotel.android.db.WarrantyHotelDBSchema.Inbox_Free_Table;
import com.warrantyhotel.android.db.WarrantyHotelDBSchema.Inbox_Information_Table;
import com.warrantyhotel.android.db.WarrantyHotelDBSchema.Inbox_RSS_Table;
import com.warrantyhotel.android.db.WarrantyHotelDBSchema.Inbox_Transactions_Table;
import com.warrantyhotel.android.db.WarrantyHotelDBSchema.Inbox_WarrantyTransfer_Table;
import com.warrantyhotel.android.db.WarrantyHotelDBSchema.Union_Inbox;
import com.warrantyhotel.android.model.Keys;
import com.warrantyhotel.android.service.UpdateIntentService;
import com.warrantyhotel.android.util.DetachableResultReceiver;
import com.warrantyhotel.android.util.DetachableResultReceiver.Receiver;
import com.warrantyhotel.android.util.NotifyingAsyncQueryHandler;
import com.warrantyhotel.android.util.NotifyingAsyncQueryHandler.AsyncQueryListener;

public class InboxActivity extends Activity implements AsyncQueryListener, Receiver, OnItemClickListener {

	private ListView listView;
	private DetachableResultReceiver receiver;
	private NotifyingAsyncQueryHandler mHandler;
	private ProgressDialog progress;
	private InboxAdapter mAdapter;
	private Cursor mCursor;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.inbox);

		mHandler = new NotifyingAsyncQueryHandler(getContentResolver(), this);
		receiver = new DetachableResultReceiver(new Handler());
		receiver.setReceiver(this);

		listView = (ListView) findViewById(R.id.inbox_list);
		listView.setOnItemClickListener(this);

		Intent testIntent = new Intent(Intent.ACTION_SYNC, null, this, UpdateIntentService.class);
		testIntent.putExtra(UpdateIntentService.EXTRA_STATUS_RECEIVER, receiver);
		testIntent.putExtra(UpdateIntentService.EXTRA_REQUEST_CODE, QueriesCodes.INBOX);
		startService(testIntent);

		progress = ProgressDialog.show(this, getString(R.string.loading), getString(R.string.please_wait), true, true);
		
		inboxRetrievingFromDB();
	}

	private void inboxRetrievingFromDB() {
		// String[] innerProjection = new String[] { BaseColumns._ID,
		// Inbox_Free_Table.GET_FREE_ID, Inbox_Free_Table.ACTION_TEXT,
		// Inbox_Free_Table.ACTION_TITLE,
		// Inbox_Free_Table.NAME_TABLE };
		// String[] innerProjection = new String[] { BaseColumns._ID,
		// Inbox_Free_Table.GET_FREE_ID, Inbox_Free_Table.ACTION_TEXT,
		// Inbox_Free_Table.ACTION_TITLE,
		// Inbox_Free_Table.NAME_TABLE };
		String[] innerProjectionInfo = new String[] { BaseColumns._ID, Inbox_Information_Table.INFO_ID, Inbox_Information_Table.TEXT,
				Inbox_Information_Table.TITLE, Inbox_Information_Table.NAME_TABLE };
		mHandler.startQuery(QueriesCodes.UNION_INBOX, Union_Inbox.CONTENT_URI, innerProjectionInfo, null, null, null);
	}

	@Override
	protected void onResume() {
		if (mCursor != null) {
			mCursor.requery();
		}
		super.onResume();
	}

	@Override
	public void onQueryComplete(int token, Object cookie, Cursor cursor) {
		switch (token) {
		case QueriesCodes.UNION_INBOX:
			if (cursor != null) {
				startManagingCursor(cursor);
				mCursor = cursor;
				if (mAdapter == null) {
					mAdapter = new InboxAdapter(getApplicationContext(), cursor);
					listView.setAdapter(mAdapter);
				} else {
					mAdapter.changeCursor(cursor);
					mAdapter.notifyDataSetChanged();
				}
			}
			break;

		default:
			break;
		}

	}

	@Override
	public void onReceiveResult(int resultCode, Bundle resultData) {
		switch (resultCode) {
		case UpdateIntentService.STATUS_RUNNING:
			break;
		case UpdateIntentService.STATUS_ERROR:
			if (progress != null)
				progress.dismiss();
			// ToastUtil.showText(getApplicationContext(),
			// resultData.getString(UpdateIntentService.ERROR_MESSAGE));
			break;
		case UpdateIntentService.STATUS_FINISHED:
			if (progress != null)
				progress.dismiss();
			inboxRetrievingFromDB();
			break;
		}

	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {
		if (null != mCursor) {
			mCursor.moveToPosition(position);
			String guid = mCursor.getString(mCursor.getColumnIndex(Inbox_Information_Table.INFO_ID));
			String name_table = mCursor.getString(mCursor.getColumnIndex(Inbox_Information_Table.NAME_TABLE));
			Log.v(name_table, name_table);
			Intent intent = null;
			if (Inbox_RSS_Table.TABLE_NAME.equals(name_table)) {
				intent = new Intent(getApplicationContext(), InboxNewsDetail.class);
			} else if (Inbox_Free_Table.TABLE_NAME.equals(name_table)) {
				intent = new Intent(getApplicationContext(), InboxGetFreeDetail.class);
			} else if (Inbox_Information_Table.TABLE_NAME.equals(name_table)) {
				intent = new Intent(getApplicationContext(), InboxInformationDetail.class);
			} else if (Inbox_Transactions_Table.TABLE_NAME.equals(name_table)) {
				intent = new Intent(getApplicationContext(), InboxTransactionDetail.class);
			} else if (Inbox_WarrantyTransfer_Table.TABLE_NAME.equals(name_table)) {
				intent = new Intent(getApplicationContext(), InboxWarrantyTransferDetail.class);
			} else if (Inbox_Account_Upload_Transfers_Table.TABLE_NAME.equals(name_table)) {
				intent = new Intent(getApplicationContext(), InboxUploadWarrantyDetail.class);
			}

			intent.putExtra(Keys._guid, guid);
			startActivity(intent);
		}
	}
}